﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WeifenLuo.WinFormsUI;

namespace GUMM
{
    public partial class MainForm : Form
    {
        private DeserializeDockContent dockInfo;
        DebugOutput DebugView;
        TextOutput ScriptView;
        MediaBrowser MediaExplorer;
        MediaEditor MediaEdit;
        MediaViewer MediaView;
        MediaLister MediaLists;

        List<MediaInfo> MediaList;


        string AppDataPath;
        string ScriptsFolder;
        string DockingConfig;

        public MainForm()
        {
            InitializeComponent();
            MediaList = new List<MediaInfo>();
            DebugView = new DebugOutput();
            ScriptView = new TextOutput();

            dockInfo = new DeserializeDockContent(GetDockingInfo);


            AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + System.IO.Path.DirectorySeparatorChar + Application.CompanyName + System.IO.Path.DirectorySeparatorChar;
            ScriptsFolder = AppDataPath + "Scripts" + System.IO.Path.DirectorySeparatorChar;
            DockingConfig = AppDataPath + "docking.xml";
            VerifyFolders();
        }

        private void InitializeDocking()
        {
        
        }

        MainForm(List<MediaInfo> mediaItems)
            :this()
        {
            this.MediaList.AddRange(mediaItems);
        }

        private IDockContent GetDockingInfo(string persistString)
        {
            if (persistString == typeof(MediaBrowser).FullName)
                return MediaExplorer;
            else if (persistString == typeof(TextOutput).FullName)
                return ScriptView;
            else if (persistString == typeof(DebugOutput).FullName)
                return DebugView;
            else if (persistString == typeof(MediaEditor).FullName)
                return MediaEdit;
            else if (persistString == typeof(MediaViewer).FullName)
                return MediaView;
            else if (persistString == typeof(MediaLister).FullName)
                return MediaLists;
            else return ScriptView;
            
        }
        
        private void MainForm_Load(object sender, EventArgs e)
        {
            MediaExplorer = new MediaBrowser(MediaList);
            ScriptView = new ScriptOutput();
            DebugView = new DebugOutput();
            MediaView = new MediaViewer();
            MediaLists = new MediaLister();
            MediaEdit = new MediaEditor();
            
            if (System.IO.File.Exists(DockingConfig))
                dpMain.LoadFromXml(DockingConfig, dockInfo);
            else
            {
                MediaExplorer.Show(dpMain, DockState.DockLeft);
                ScriptView.Show(dpMain, DockState.DockBottomAutoHide);
                DebugView.Show(dpMain, DockState.DockBottomAutoHide);
                MediaView.Show(dpMain, DockState.Document);
                MediaEdit.Show(dpMain, DockState.Document);
            }
        }

        private void VerifyFolders()
        {
            if (!System.IO.Directory.Exists(AppDataPath))
                System.IO.Directory.CreateDirectory(AppDataPath);
            if (!System.IO.Directory.Exists(ScriptsFolder))
                System.IO.Directory.CreateDirectory(ScriptsFolder);
        }

        private void dpMain_ActiveContentChanged(object sender, EventArgs e)
        {

        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            dpMain.SaveAsXml(DockingConfig);
        }

        private void tsddView_DropDownOpening(object sender, EventArgs e)
        {
            if (ScriptView.IsHidden)
            {
                tsmiScriptLog.Checked = false;
            }
        }

        private void tsmiScriptLog_Click(object sender, EventArgs e)
        {
            if (ScriptView.IsHidden)
            {
                ScriptView.Show(dpMain);
                ScriptView.IsHidden = !ScriptView.IsHidden;
            }
        }
    }
}
